package com.huishen.edrivenew;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.huishen.edrivenew.bean.CoachListBean;
import com.huishen.edrivenew.bean.DateBean;
import com.huishen.edrivenew.bean.LessInfoBean;
import com.huishen.edrivenew.bean.ListLessonInfoBean;
import com.huishen.edrivenew.bean.OutlineBean;
import com.huishen.edrivenew.bean.StudentBean;
import com.huishen.edrivenew.net.ApiHttpClient;
import com.huishen.edrivenew.ui.MainActivity;
import com.huishen.edrivenew.util.StringUtils;
import com.huishen.edrivenew.widget.CalendarGridAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore;

import java.util.Properties;
import java.util.UUID;

/**
 * 应用程序类
 */
public class AppContext extends Application {

    private static AppContext instance;

    static Context _context;
    static Resources _resource;
    
    public StudentBean studentBean;
    public DateBean[] days;
    public ListLessonInfoBean _7DaysInfo;
    
    public OutlineBean outline;
    
    public CalendarGridAdapter caAdapter;
    public MainActivity mainActivity;
    //约课的日期
    public DateBean dateBean;

    public LessInfoBean judgeBean;
    
    public String mobileFlag = null;
    
    // 旧模式 预约统计页面 点击取消 传送日期参数
    public String oldClassListDate = "";
    
    // 新模式约课列表状态
    public int classActivityType = 0;
    
 // 屏幕信息
    private DisplayMetrics metric;
    /**
     *  获取屏幕信息
     *  metric.widthPixels 屏幕宽度（像素）;
     *  height = metric.heightPixels 屏幕高度（像素）;
     *  float density = metric.density  屏幕密度（0.75 / 1.0 / 1.5）;
     *  int densityDpi = metric.densityDpi  屏幕密度DPI（120 / 160 / 240）;
     */
    public DisplayMetrics displayMetrics;

    /**
     * 获得当前app运行的AppContext
     */
    public static AppContext getInstance() {
        return instance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        _context = getApplicationContext();
        _resource = _context.getResources();

        instance = this;

        init();
    }


    public static synchronized AppContext context() {
        return (AppContext) _context;
    }

    public static Resources resources() {
        return _resource;
    }


    private void init() {
        // 初始化网络请求
        AsyncHttpClient client = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
        client.setCookieStore(myCookieStore);
        ApiHttpClient.setHttpClient(client);
    }


    /**
     * 获取App唯一标识
     *
     * @return
     */
    public String getAppId() {
        if(! StringUtils.isEmpty(mobileFlag)) {
            return mobileFlag;
        }
        mobileFlag = getProperty(AppConfig.CONF_APP_UNIQUEID);
        if (StringUtils.isEmpty(mobileFlag)) {
            mobileFlag = UUID.randomUUID().toString();
            setProperty(AppConfig.CONF_APP_UNIQUEID, mobileFlag);
        }
        return mobileFlag;
    }
    
    

    public void resetAppId() {
        this.mobileFlag = null;
        setProperty(AppConfig.CONF_APP_UNIQUEID, "");
    }


    public boolean containsProperty(String key) {
        Properties props = getProperties();
        return props.containsKey(key);
    }

    public void setProperties(Properties ps) {
        AppConfig.getAppConfig(this).set(ps);
    }

    public Properties getProperties() {
        return AppConfig.getAppConfig(this).get();
    }

    public void setProperty(String key, String value) {
        AppConfig.getAppConfig(this).set(key, value);
    }
    
    
    public void saveStudent(StudentBean bean) {
        setProperty("student", bean.toStringObj());
    }
    
    public StudentBean getStudent() {
        String stu = getProperty("student");
        StudentBean stuBean = StudentBean.getFromJson(stu);
        this.studentBean = stuBean;
        return studentBean;
    }
    
    public void saveLogin(String i) {
        setProperty("login", i);
    }
    
    public boolean getLogin() {
        String login = getProperty("login");
        if(StringUtils.isEmpty(login) || !login.equals("1")) {
            return false;
        }
        return true;
    }
    
    
    public void saveUserName(String username) {
        setProperty("username", username);
    }
    
    public String getUserName() {
        return getProperty("username");
    }
    
    
    
    /**
     *  获取屏幕信息
     *  metric.widthPixels 屏幕宽度（像素）;
     *  height = metric.heightPixels 屏幕高度（像素）;
     *  float density = metric.density  屏幕密度（0.75 / 1.0 / 1.5）;
     *  int densityDpi = metric.densityDpi  屏幕密度DPI（120 / 160 / 240）;
     */
    public DisplayMetrics getScreenInfo(Activity activity) {
        if(metric == null) {
            metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        }
        this.displayMetrics = metric;
        return metric;
    }
    
    
    /**
     * 获取cookie时传AppConfig.CONF_COOKIE
     *
     * @param key
     * @return
     */
    public String getProperty(String key) {
        String res = AppConfig.getAppConfig(this).get(key);
        return res;
    }

    public void removeProperty(String... key) {
        AppConfig.getAppConfig(this).remove(key);
    }

    /**
     * 获取App安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }

    public static boolean isMethodsCompat(int VersionCode) {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        return currentVersion >= VersionCode;
    }

}
